package com.java.exer1;

import javax.swing.JOptionPane;

public class Test01 {

    public static void main(String[] args) {

        String input = JOptionPane.showInputDialog("输入 数值,偏移量,长度");

        if (input == null) {
            JOptionPane.showMessageDialog(null, "空对象");
            return;
        }
        String[] inputWords = input.split(",");

        if (inputWords.length != 3) {

            JOptionPane.showMessageDialog(null, "格式错误");
            return;
        }

        if (!isNumber(inputWords[0], inputWords[1], inputWords[2])) {
            JOptionPane.showMessageDialog(null, "输入的数值有误");
            return;
        }

        int begin = Integer.parseInt(inputWords[1]);
        int end = begin + Integer.parseInt(inputWords[2]);
        if (isOutOfBounds(inputWords, begin, end)) {
            JOptionPane.showMessageDialog(null, "长度越界");
            return;
        }


        String subStr = inputWords[0].substring(begin, end);

        String unit = "零壹贰叁肆伍陆柒扒玖";
        String result = "";
        for (int i = 0; i < subStr.length(); i++) {
            char ac = subStr.charAt(i);
            result += unit.charAt(ac - 48);
        }

        System.out.println(result);
    }

    private static boolean isNumber(String... str) {
        for (int i = 0; i < str.length; i++) {
            if (Integer.parseInt(str[i]) > 48 || Integer.parseInt(str[i]) < 58) {
                return false;
            }
        }
        return true;
    }

    private static boolean isOutOfBounds(String[] inputWords, int begin, int end) {
        //to do
        if (inputWords.length - 1 < end | inputWords.length < begin + end) {
            return true;
        }
        return false;
    }

}
